package org.bookshare.validator.error;

import org.benetech.beans.error.ValidationError;
import org.benetech.beans.error.ValidationError.Severity;

/**
 * An expected element is missing from an xml file.
 * @author Reuben Firmin
 */
public class MissingElementValidationError implements ValidationError {

    private String elementName;
    private String attributeName;
    private String attributeValue;
    private String parentName;
    private String context;
    private Severity severity;

    /**
     * Construct a missing element validation.
     * @param elementName the name of the element that is missing
     * @param parentName the parent element name
     * @param context the context, i.e. file name
     * @param severity the severity of the violation
     */
    public MissingElementValidationError(final String elementName,
        final String parentName, final String context,
        final Severity severity)
    {
        this.elementName = elementName;
        this.parentName = parentName;
        this.context = context;
        this.severity = severity;
    }

    /**
     * Construct a missing element validation. This variant requires that an
     * attribute of the given element be set to a specified value.
     * @param elementName the name of the element that is missing
     * @param attributeName the name of the attribute that should be set
     * @param attributeValue the required value of the attribute
     * @param parentName the parent element name
     * @param context the context, i.e. file name
     * @param severity the severity of the violation
     */
    public MissingElementValidationError(final String elementName,
        final String attributeName, final String attributeValue,
        final String parentName, final String context,
        final Severity severity)
    {
        this.elementName = elementName;
        this.attributeName = attributeName;
        this.attributeValue = attributeValue;
        this.parentName = parentName;
        this.context = context;
        this.severity = severity;
    }

    /**
     * The context of the missing element.
     * @return never null
     */
    public final String getContext() {
        return context;
    }

    /**
     * The missing element's name.
     * @return never null
     */
    public final String getElementName() {
        return elementName;
    }

    /**
     * The parent element name.
     * @return never null
     */
    public final String getParentName() {
        return parentName;
    }

    /**
     * The attribute name that should be set on the element.
     * @return null if not required.
     */
    public final String getAttributeName() {
        return attributeName;
    }

    /**
     * The attribute value that should be specified.
     * @return not null, if attribute name is set.
     */
    public final String getAttributeValue() {
        return attributeValue;
    }

    /**
     * {@inheritDoc}
     */
    public final Severity getSeverity() {
        return severity;
    }

    /**
     * {@inheritDoc}
     */
    public final String toString() {
        if (attributeName == null) {
            return "Expected element: " + elementName
                + " missing from element: " + parentName + " in file: "
                + context;
        } else {
            return "Expected element: " + elementName + " with attribute: "
                + attributeName + "=\"" + attributeValue
                + "\" within element: " + parentName + " in file: " + context;
        }
     }
}
